﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Business;
using Entity;
using Entity.Custom;
using Newtonsoft.Json;
using Utilities;

namespace FreshCms.BackEnd.Handler
{
    /// <summary>
    /// Summary description for Gallery
    /// </summary>
    public class Gallery : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        #region Declare variable for check pms
        Int32 ui = 0;
        InPms chkPms = new InPms();
        InUser CurrentUser = new InUser();
        #endregion

        public void ProcessRequest(HttpContext context)
        {
            string m = "";
            var response = new ResponseData();

            #region Get PanelId and Check Psm
            CurrentUser = InUserBusiness.GetSessionInUser();
            if (!string.IsNullOrEmpty(context.Request["ui"]))
            {
                Int32.TryParse(context.Request["ui"], out ui);
                if (ui > 0)
                    chkPms = InPmsBusiness.CheckSessionPms(ui);
                if (chkPms == null)
                    chkPms = new InPms { PmsView = 0, PmsUpdate = 0, PmsDelete = 0 };
            }
            #endregion



            if (context.Request.RequestType == "POST")
            {
                if (context.Request["m"] != null)
                    m = context.Request["m"];
                else
                    return;
            }
            else
            {
                if (context.Request.QueryString["m"] != null)
                    m = context.Request.QueryString["m"];
                else
                    return;
            }

            switch (m.ToLower())
            {
                case "delete":
                    response = Delete(context);
                    break;
                case "deletemulti":
                    response = DeleteMulti(context);
                    break;
                    
            }

            context.Response.ContentType = "application/x-javascript";

            string callBack = context.Request["callback"];
            if (!string.IsNullOrEmpty(callBack))
            {
                context.Response.Write(callBack + "( " + JsonConvert.SerializeObject(response) + " );");
            }
            else
                context.Response.Write(JsonConvert.SerializeObject(response));
        }

        private ResponseData Delete(HttpContext context)
        {
            // Check Pms
            if (chkPms.PmsDelete == 0)
                return new ResponseData { Data = null, ExtData = null, Message = Constant.PmsNoDelete, Success = false };

            var response = new ResponseData();
            if (!string.IsNullOrEmpty(context.Request["id"]))
            {
                var id = 0;
                Int32.TryParse(context.Request["id"], out id);
                if (id > 0)
                {
                    response.Data = OutGalleryBusiness.DeleteOutGallery(id);
                    response.Message = "Call Delete OK";
                    response.Success = true;
                    // log system 
                    InLogBusiness.InsertInLog(new InLog
                    {
                        UserId = CurrentUser.UserId,
                        LogAction = 0,
                        Field1 = "Xóa ảnh gallery thành công",
                        Field2 = "",
                        Field3 = "",
                        LogId = 0
                    });
                }
                else
                {
                    response.Data = id;
                    response.Message = "id <=0";
                    response.Success = false;
                }
            }
            else
            {
                response.Data = -1;
                response.Message = "Param not valid";
                response.Success = false;
            }
            return response;
        }

        private ResponseData DeleteMulti(HttpContext context)
        {
            // Check Pms
            if (chkPms.PmsDelete == 0)
                return new ResponseData { Data = null, ExtData = null, Message = Constant.PmsNoDelete, Success = false };

            var response = new ResponseData();
            if (!string.IsNullOrEmpty(context.Request["lstid"]))
            {
                string lstid = context.Request["lstid"];
                if (lstid.Length > 0)
                {
                    response.Data = OutGalleryBusiness.DeleteMultiOutGallery(lstid);
                    response.Message = "Call Delete OK";
                    response.Success = true;
                    // log system 
                    InLogBusiness.InsertInLog(new InLog
                    {
                        UserId = CurrentUser.UserId,
                        LogAction = 0,
                        Field1 = "Xóa nhiều ảnh gallery thành công",
                        Field2 = "",
                        Field3 = "",
                        LogId = 0
                    });
                }
                else
                {
                    response.Data = lstid;
                    response.Message = "lstid.Length <=0";
                    response.Success = false;
                }
            }
            else
            {
                response.Data = -1;
                response.Message = "Param not valid";
                response.Success = false;
            }
            return response;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}